from command import CMD
from functools import partial

class HANDLERS(dict):
    def __call__(self, cmd_type):
        """Decorate this object directly to register `func` to 
        the `cmd_type`.

        Parameters
        ----------
        cmd_type : `CMD.Type`
            Command type to bind.
        """
        def wrapper(func):
            self[cmd_type] = func
        return wrapper


class WORKER:

    # Subclass MUST overide handlers.
    handlers = HANDLERS()
    tasks = []
    is_running = False
    def __init__(self) -> None:
        self._init_bind()
    
    def _init_bind(self):
        """Call when WORKER object init, will bind funcs which `_HANDLERS` object
        to the command.
        """
        for type_, func in self.handlers.items():
            CMD.add_handler(type_, partial(func, self))